﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Objects;
using WISD.CarFriend.Model;
using WISD.Common;

namespace WISD.CarFriend.BLL.Web
{
    public class AdvertisementManageModule
    {
        /// <summary>
        /// 获取所有广告
        /// state:1 未删除
        /// state:0 已删除
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllAdvertisements()
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                string esql = "select value a from T_CF_Web_AdvertisementInfo as a where a.state=1 ";
                ObjectQuery<T_CF_Web_AdvertisementInfo> query = edm.CreateQuery<T_CF_Web_AdvertisementInfo>(esql);
                return EFHelper.ListToDataTable<T_CF_Web_AdvertisementInfo>(query.Execute(MergeOption.NoTracking).ToList<T_CF_Web_AdvertisementInfo>());
            }
        }


        /// <summary>
        /// 根据ID查找广告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T_CF_Web_AdvertisementInfo GetAdvertisementByID(int id)
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                return edm.T_CF_Web_AdvertisementInfo.FirstOrDefault(p => p.AdInfoID == id);
            }
        }


        /// <summary>
        /// 添加广告
        /// </summary>
        /// <param name="title"></param>
        /// <param name="imagePath"></param>
        /// <param name="url"></param>
        /// <param name="creatorID"></param>
        public void AddAdvertisement(string title, string imagePath, string url, int creatorID)
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                T_CF_Web_AdvertisementInfo adInfo = new T_CF_Web_AdvertisementInfo()
                {
                    Title = title,
                    ImagePath = imagePath,
                    URL = url,
                    State = 1,
                    CreatorID = creatorID,
                    CreateTime = DateTime.Now,
                    UpdaterID = creatorID,
                    UpdateTime = DateTime.Now,
                    Remarks = ""
                };
                edm.T_CF_Web_AdvertisementInfo.AddObject(adInfo);
                edm.SaveChanges();
            }
        }


        /// <summary>
        /// 修改广告
        /// </summary>
        /// <param name="id"></param>
        /// <param name="title"></param>
        /// <param name="imagePath"></param>
        /// <param name="url"></param>
        /// <param name="updaterID"></param>
        public void UpdateAdvertisement(int id, string title, string imagePath, string url, int updaterID)
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                T_CF_Web_AdvertisementInfo adInfo = edm.T_CF_Web_AdvertisementInfo.FirstOrDefault(p => p.AdInfoID == id);
                adInfo.Title = title;
                adInfo.ImagePath = imagePath;
                adInfo.URL = url;
                adInfo.UpdaterID = updaterID;
                adInfo.UpdateTime = DateTime.Now;
                edm.SaveChanges();
            }
        }


        /// <summary>
        /// 删除广告
        /// </summary>
        /// <param name="id"></param>
        /// <param name="updaterID"></param>
        public void DelAdvertisement(int id, int updaterID)
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                T_CF_Web_AdvertisementInfo adInfo = edm.T_CF_Web_AdvertisementInfo.FirstOrDefault(p => p.AdInfoID == id);
                adInfo.State = 0;
                adInfo.UpdaterID = updaterID;
                adInfo.UpdateTime = DateTime.Now;
                edm.SaveChanges();
            }
        }


        /// <summary>
        /// 获取首页广告FlashXML
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <param name="num">显示字数</param>
        /// <returns></returns>
        public string GetFlashXML(int top, int num)
        {
            StringBuilder xml = new StringBuilder();

            DataTable AdTable = new DataTable();
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                string esql = "select value a from T_CF_Web_AdvertisementInfo as a where a.state=1 order by a.AdInfoID skip 0 limit " + top.ToString();
                ObjectQuery<T_CF_Web_AdvertisementInfo> query = edm.CreateQuery<T_CF_Web_AdvertisementInfo>(esql);
                AdTable = EFHelper.ListToDataTable<T_CF_Web_AdvertisementInfo>(query.Execute(MergeOption.NoTracking).ToList<T_CF_Web_AdvertisementInfo>());
            }

            if (AdTable.Rows.Count > 0)
            {
                for (int i = 0; i < AdTable.Rows.Count; i++)
                {
                    DataRow dr = AdTable.Rows[i];
                    xml.Append("<item>");
                    xml.Append("<link>" + dr["URL"].ToString() + "</link>");
                    xml.Append("<image>" + StringHandle.GetBathPath() + dr["ImagePath"].ToString() + "</image>");
                    xml.Append("<title>" + StringHandle.CutString(dr["Title"].ToString(), num) + "</title>");
                    xml.Append("</item>");
                }
            }
            else
            {
                xml.Append("");
            }

            return xml.ToString();
        }

    }
}
